<?php

/**
*@作者:MissZhang
*@邮箱:<787727147@qq.com>
*@创建时间:2021/7/6 下午12:04
*@说明:order模型
*/
namespace app\common\model;


use think\facade\Db;
use think\Model;

class Order extends OrderCommon
{
    protected $pk = "order_id";
    // 定义时间戳字段名
    protected $createTime = 'add_time';
    //修改format类型
    protected $dateFormat = "Y-m-d H:i:s";
    protected $type=[
        'total_amount'  =>  'float',
        'order_amount'  =>  'float',
    ];
    public function getPkAttr($value,$data) {
        return $this->pk;
    }
    public function user(){
        return $this->hasOne(Users::class, 'user_id','user_id');
    }
    public function store()
    {
        return $this->hasOne(Store::class, 'store_id', 'store_id');
    }
    public function orderGoods(){
        return $this->hasMany(OrderGoods::class, 'order_id','order_id');
    }
    public function getGoodsNumAttr($value,$data) {
        $goods_where[]=['order_id','=',$data['order_id']];
        $goods_where[]=['is_zhifu','=','1'];
        $goods_num=Db::name('order_goods')->where($goods_where)->sum('goods_num');
        return $goods_num;
    }
    public function getCostPriceAttr($value,$data) {
        $goods_where[]=['order_id','=',$data['order_id']];
        $goods_where[]=['is_zhifu','=','1'];
        $cost_price=Db::name('order_goods')->where($goods_where)->sum('goods_num*cost_price');
        return $cost_price;
    }
    public function getTypeTextAttr($value,$data) {
        $types = [1=>'普通订单',2=>'升级订单'];
        return $types[$data['type']];
    }
    public function getOrderStatusTextAttr($value,$data) {
        $status = [-2=>'已取消',-1=>'待支付',1=>'待发货',2=>'待收货',3=>'已完成'];
        return $status[$data['order_status']];
    }
    public function getConsigneeTextAttr($value,$data) {
        return $data['consignee'].":".$data['mobile'];
    }
    public function getPayTimeTextAttr($value,$data) {
        if ($data['pay_time']){
            return date($this->dateFormat,$data['pay_time']);
        }
        return '---';
    }
    public function getConfirmTimeTextAttr($value,$data) {
        if ($data['confirm_time']){
            return date($this->dateFormat,$data['confirm_time']);
        }
        return '---';
    }
    public function getShippingTimeTextAttr($value,$data) {
        if ($data['shipping_time']){
            return date($this->dateFormat,$data['shipping_time']);
        }
        return '---';
    }
    public function getCancelTimeTextAttr($value,$data) {
        if ($data['cancel_time']){
            return date($this->dateFormat,$data['cancel_time']);
        }
        return '---';
    }
    public function getAllAddressAttr($value,$data) {
        return $data['province'].$data['city'].$data['district'].$data['address'];
    }
}
